/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SearchPanel.java
 *
 * Created on 2009-05-01, 22:06:31
 */
package gui;

import java.awt.FocusTraversalPolicy;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.text.DefaultCaret;
import org.jdesktop.application.Action;

/**
 *
 * @author soldier
 */
public class SearchPanel extends javax.swing.JPanel {

    private String panelName;
    private int id;
    private int popupMenuX;
    private int popupMenuY;

    /** Creates new form SearchPanel */
    public SearchPanel(String name, int id) {
        panelName = name;
        this.id = id;
        initComponents();
    }

    @Action
    public void closeSearchPanel() {
        //P2pApp.getManager().closePanel(id);
        P2pApp.getManager().remSearchPanel(id);

    }

    private void unlockSearchActionGen() {
        okButton.setEnabled(true);
        filenameTextField.setEnabled(true);
    }

    @Action
    public void dataAccepted() {
        okButton.setEnabled(false);
        filenameTextField.setEnabled(false);
        Float min, max;
        int minInt = 0,
                maxInt = Integer.MAX_VALUE;

        if (!filenameTextField.getText().matches("[^<>]+")) {
            JOptionPane.showMessageDialog(P2pApp.getApplication().getMainFrame(),
                    "Filename is wrong",
                    "Wrong format",
                    JOptionPane.ERROR_MESSAGE);
            unlockSearchActionGen();
            return;
        }
        if (minCheckBox.isSelected()) {
            try {
                min = new Float(minTextField.getText());
                minInt = calculateSize(minComboBox, min);
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(P2pApp.getApplication().getMainFrame(),
                        "Min value must be a number",
                        "WronJOptionPag format",
                        JOptionPane.ERROR_MESSAGE);
                unlockSearchActionGen();
                return;
            }
        }
        if (maxCheckBox.isSelected()) {
            try {
                max = new Float(maxTextField.getText());
                maxInt = calculateSize(maxComboBox, max);
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(P2pApp.getApplication().getMainFrame(),
                        "Max value must be a number",
                        "Wrong format",
                        JOptionPane.ERROR_MESSAGE);
                unlockSearchActionGen();
                return;
            }
        }
        if (minInt < 0 || maxInt < 0) {
            JOptionPane.showMessageDialog(P2pApp.getApplication().getMainFrame(),
                    "Size must be a non negative number",
                    "Wrong value",
                    JOptionPane.ERROR_MESSAGE);
            unlockSearchActionGen();
            return;
        }

        if (minInt > maxInt) {
        }
        minCheckBox.setEnabled(false);
        minComboBox.setEnabled(false);
        minTextField.setEnabled(false);
        maxComboBox.setEnabled(false);
        maxCheckBox.setEnabled(false);
        maxTextField.setEnabled(false);
        P2pApp.getManager().outSearchFile(id, minInt, maxInt, filenameTextField.getText());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SearchPopupMenu = new javax.swing.JPopupMenu();
        SearchMenuDownload = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        filenameTextField = new javax.swing.JTextField();
        minTextField = new javax.swing.JTextField();
        minComboBox = new javax.swing.JComboBox();
        minCheckBox = new javax.swing.JCheckBox();
        maxCheckBox = new javax.swing.JCheckBox();
        maxTextField = new javax.swing.JTextField();
        maxComboBox = new javax.swing.JComboBox();
        okButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        SearchTable = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();

        SearchPopupMenu.setName("SearchPopupMenu"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gui.P2pApp.class).getContext().getResourceMap(SearchPanel.class);
        SearchMenuDownload.setText(resourceMap.getString("SearchMenuDownload.text")); // NOI18N
        SearchMenuDownload.setName("SearchMenuDownload"); // NOI18N
        SearchMenuDownload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchMenuDownloadActionPerformed(evt);
            }
        });
        SearchPopupMenu.add(SearchMenuDownload);

        setName(panelName);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        filenameTextField.setText(resourceMap.getString("filenameTextField.text")); // NOI18N
        filenameTextField.setName("filenameTextField"); // NOI18N
        filenameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filenameTextFieldActionPerformed(evt);
            }
        });

        minTextField.setText(resourceMap.getString("minTextField.text")); // NOI18N
        minTextField.setEnabled(minCheckBox.isSelected());
        minTextField.setName("minTextField"); // NOI18N

        minComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "kB", "MB", "GB" }));
        minComboBox.setEnabled(false);
        minComboBox.setName("minComboBox"); // NOI18N

        minCheckBox.setText(resourceMap.getString("minCheckBox.text")); // NOI18N
        minCheckBox.setName("minCheckBox"); // NOI18N
        minCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minCheckBoxClicked(evt);
            }
        });

        maxCheckBox.setText(resourceMap.getString("maxCheckBox.text")); // NOI18N
        maxCheckBox.setName("maxCheckBox"); // NOI18N
        maxCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maxCheckBoxClicked(evt);
            }
        });

        maxTextField.setText(resourceMap.getString("maxTextField.text")); // NOI18N
        maxTextField.setEnabled(maxCheckBox.isSelected());
        maxTextField.setName("maxTextField"); // NOI18N

        maxComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "kB", "MB", "GB" }));
        maxComboBox.setEnabled(false);
        maxComboBox.setName("maxComboBox"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gui.P2pApp.class).getContext().getActionMap(SearchPanel.class, this);
        okButton.setAction(actionMap.get("dataAccepted")); // NOI18N
        okButton.setIcon(resourceMap.getIcon("okButton.icon")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setToolTipText(resourceMap.getString("okButton.toolTipText")); // NOI18N
        okButton.setBorderPainted(false);
        okButton.setContentAreaFilled(false);
        okButton.setFocusPainted(false);
        okButton.setName("okButton"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        SearchTable.setModel(P2pApp.getManager().getSearchModel(id));
        SearchTable.setComponentPopupMenu(SearchPopupMenu);
        SearchTable.setName("SearchTable"); // NOI18N
        SearchTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SearchTableMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                SearchTableMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(SearchTable);

        jButton2.setAction(actionMap.get("closeSearchPanel")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setToolTipText(resourceMap.getString("jButton2.toolTipText")); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.setFocusPainted(false);
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(filenameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(maxCheckBox)
                            .addComponent(minCheckBox))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(minTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
                            .addComponent(maxTextField, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(maxComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(minComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(31, 31, 31)
                        .addComponent(okButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(filenameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(minCheckBox)
                                    .addComponent(minTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(minComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(maxCheckBox)
                                    .addComponent(maxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(maxComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void minCheckBoxClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minCheckBoxClicked
        // TODO add your handling code here:
        minTextField.setEnabled(minCheckBox.isSelected());
        minComboBox.setEnabled(minCheckBox.isSelected());
    }//GEN-LAST:event_minCheckBoxClicked

    private void maxCheckBoxClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maxCheckBoxClicked
        // TODO add your handling code here:
        maxTextField.setEnabled(maxCheckBox.isSelected());
        maxComboBox.setEnabled(maxCheckBox.isSelected());
    }//GEN-LAST:event_maxCheckBoxClicked

    private void SearchMenuDownloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchMenuDownloadActionPerformed
        // TODO add your handling code here:
        startDownload(popupMenuX, popupMenuY);
    }//GEN-LAST:event_SearchMenuDownloadActionPerformed

    private void SearchTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SearchTableMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            startDownload(evt.getX(), evt.getY());
        }
}//GEN-LAST:event_SearchTableMouseClicked

    private void SearchTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SearchTableMouseReleased
        // TODO add your handling code here:
        popupMenuX = evt.getX();
        popupMenuY = evt.getY();
    }//GEN-LAST:event_SearchTableMouseReleased

    private void filenameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filenameTextFieldActionPerformed
        // TODO add your handling code here:
        dataAccepted();
    }//GEN-LAST:event_filenameTextFieldActionPerformed

    private void startDownload(int x, int y) {
        System.out.println("X=" + x + " Y=" + y);
        int idRow = y / SearchTable.getRowHeight();
        System.out.println("Row=" + idRow);
        P2pApp.getManager().outAddDownload(id, idRow);
    }

    private int calculateSize(JComboBox box, Float value) {
        if (box.getSelectedIndex() == 0) {
            return value.intValue() * 1024;
        } else if (box.getSelectedIndex() == 1) {
            return value.intValue() * 1024 * 1024;
        } else if (box.getSelectedIndex() == 2) {
            return value.intValue() * 1024 * 1024;
        }
        System.err.println("GUI: SearchPanel: calculateSize(): unknown state");
        return 0;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem SearchMenuDownload;
    private javax.swing.JPopupMenu SearchPopupMenu;
    private javax.swing.JTable SearchTable;
    private javax.swing.JTextField filenameTextField;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JCheckBox maxCheckBox;
    private javax.swing.JComboBox maxComboBox;
    private javax.swing.JTextField maxTextField;
    private javax.swing.JCheckBox minCheckBox;
    private javax.swing.JComboBox minComboBox;
    private javax.swing.JTextField minTextField;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
}
